package generic.gui.util;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

@SuppressWarnings("serial")
public class JProgressBarForm extends JDialogForm {
	
	public final static int COMPRIMENTO_JANELA = 400;
	public final static int ALTURA_JANELA = 110;
	public static final int INICIO_COMPONENTE = 20;
	
	private JProgressBar progress;
	@SuppressWarnings("rawtypes")
	private SwingWorker worker;
	private int max;
	private int min;
	private JLabelForm label;
	private String strLabel;

	public JProgressBarForm(String title, String labelProgress, int min, int max) {
		setSize(COMPRIMENTO_JANELA, ALTURA_JANELA);
		setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);
		setTitle(title);
		setMax(max);
		setMin(min);
		setStrLabel(labelProgress);
		
		JPanel panel = new JPanel();
		panel.add(getProgress());
		panel.setLayout(null);
		panel.setBounds(INICIO_COMPONENTE, 35, COMPRIMENTO_JANELA - 2*INICIO_COMPONENTE, 30);
		
		
		
		add(getLabel());
		add(panel);
		
		setVisible(true);
		setModal(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	public JProgressBar getProgress() {
		if (progress == null) {
			progress = new JProgressBar();
			progress.setMinimum(getMin());
			progress.setMaximum(getMax());
			progress.setValue(progress.getValue());
			progress.setBounds(0, 0, COMPRIMENTO_JANELA - 2*INICIO_COMPONENTE, 30);
			progress.setStringPainted(true);
		}
		return progress;
	}
	
	public int getMax() {
		return max;
	}
	public void setMax(int max) {
		this.max = max;
	}
	public int getMin() {
		return min;
	}
	public void setMin(int min) {
		this.min = min;
	}
	public void setProgress(JProgressBar progress) {
		this.progress = progress;
	}
	public String getStrLabel() {
		return strLabel;
	}
	public void setStrLabel(String strLabel) {
		this.strLabel = strLabel;
	}
	public JLabelForm getLabel() {
		if (label == null) {
			label = new JLabelForm(getStrLabel());
			label.setFont(new Font("Arial", Font.PLAIN, 13));
			label.setLayout(null);
			label.setBounds(INICIO_COMPONENTE, 12, COMPRIMENTO_JANELA - 2*INICIO_COMPONENTE, 30);
		}
		return label;
	}
	public void setLabel(JLabelForm label) {
		this.label = label;
	}
	@SuppressWarnings("rawtypes")
	public SwingWorker getWorker() {
		if (worker == null) {
			worker = new SwingWorker() {

				@Override
				protected Object doInBackground() throws Exception {
					System.out.println("Metodo getWorker nao implementado!");
					worker = null;
					return null;
				}
			};
		}
		return worker;
	}
	@SuppressWarnings("rawtypes")
	public void setWorker(SwingWorker worker) {
		this.worker = worker;
	}
	public void execute() {
		getWorker().execute();
	}

}
